package com.contactmgr.activity;

import java.util.List;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.TabHost;

import com.contactmgr.R;
import com.contactmgr.logObjects.CallDataLog;
import com.contactmgr.logObjects.Contact;
import com.contactmgr.logObjects.Sms;
import com.contactmgr.utils.CallLogReader;
import com.contactmgr.utils.ContactNameResolver;
import com.contactmgr.utils.ContactReader;
import com.contactmgr.utils.ReadDataAppContext;
import com.contactmgr.utils.SmsReader;

public class DisplayAllLogActivity extends TabActivity implements GestureOverlayView.OnGesturePerformedListener  {
	private static final String SMS_LOG = "SmsLog";
	private static final String CALL_LOG = "CallLog";
	private static final String CONTACT = "Contact";
	List<Sms> inboxResolved;
	List<Sms> sentItemsResolved;
	List<Contact> contactList;
	List<CallDataLog> nameResolvedAllCallData;
	List<CallDataLog> nameResolvedMissedCallData;
	List<CallDataLog> nameResolvedIncomingCallData;
	List<CallDataLog> nameResolvedOutGoingCallData;
	private TabHost tabHost;
	GestureLibrary mLibrary;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainlayout);
		readLogs();
		showDataInTabsWithSeparateActivities();
		initGesture();
	}

	private void initGesture() {
		//gesture;
		GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestures);
		gesturesView.addOnGesturePerformedListener(this);
		gesturesView.setGestureVisible(false);
		mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
		if (!mLibrary.load()) {
			//error , so terminate app
		    finish();
		}
	}

	private void readLogs() {

		contactList = ContactReader.getContacts(getContentResolver());

		List<CallDataLog> callLog = CallLogReader.getAllCallLogs(getContentResolver());
		List<CallDataLog> missedCallLog = CallLogReader.getMissedCallLogs(callLog);
		List<CallDataLog> incomingCallLog = CallLogReader.getIncomingCallLogs(callLog);
		List<CallDataLog> outGoingCallLog = CallLogReader.getOutGoingCallLogs(callLog);
		nameResolvedAllCallData = ContactNameResolver.resolveNamesInCallLog(contactList, callLog);
		nameResolvedMissedCallData = ContactNameResolver.resolveNamesInCallLog(contactList, missedCallLog);
		nameResolvedIncomingCallData = ContactNameResolver.resolveNamesInCallLog(contactList, incomingCallLog);
		nameResolvedOutGoingCallData = ContactNameResolver.resolveNamesInCallLog(contactList, outGoingCallLog);
		List<Sms> inbox = SmsReader.readInbox(getContentResolver());
		List<Sms> sentItems = SmsReader.readSentItems(getContentResolver());
		inboxResolved = ContactNameResolver.resolveNamesInSms(contactList, inbox);
		sentItemsResolved = ContactNameResolver.resolveNamesInSms(contactList, sentItems);

		// set in global app context
		((ReadDataAppContext) getApplicationContext()).setContactList(contactList);
		((ReadDataAppContext) getApplicationContext()).setNameResolvedAllCallData(nameResolvedAllCallData);
		((ReadDataAppContext) getApplicationContext()).setNameResolvedMissedCallData(nameResolvedMissedCallData);
		((ReadDataAppContext) getApplicationContext()).setNameResolvedIncomingCallData(nameResolvedIncomingCallData);
		((ReadDataAppContext) getApplicationContext()).setNameResolvedOutGoingCallData(nameResolvedOutGoingCallData);

		((ReadDataAppContext) getApplicationContext()).setInboxResolved(inboxResolved);
		((ReadDataAppContext) getApplicationContext()).setSentItemsResolved(sentItemsResolved);
		for (CallDataLog callDataLog : nameResolvedAllCallData) {
			System.out.println(callDataLog.toString());
		}
		for (Contact contact : contactList) {
			System.out.println(contact.toString());
		}
	}

	private void showDataInTabsWithSeparateActivities() {
		Resources res = getResources();
		tabHost = getTabHost();
		Intent smsIntent;
		Intent callLogIntent;
		Intent contactIntent;
		// Create an Intent to launch an Activity for the tab (to be reused)
		smsIntent = new Intent().setClass(this, SmsLogActivity.class);
		callLogIntent = new Intent().setClass(this, CallLogActivity.class);
		contactIntent = new Intent().setClass(this, ContactLogActivity.class);
		// add views to tab host
		tabHost.addTab(tabHost.newTabSpec(CONTACT).setIndicator(CONTACT, res.getDrawable(R.drawable.contact_icon)).setContent(contactIntent));
		tabHost.addTab(tabHost.newTabSpec(CALL_LOG).setIndicator(CALL_LOG, res.getDrawable(R.drawable.call_icon)).setContent(callLogIntent));
		tabHost.addTab(tabHost.newTabSpec(SMS_LOG).setIndicator(SMS_LOG, res.getDrawable(R.drawable.message_icon)).setContent(smsIntent));
		tabHost.setCurrentTab(0);
	}


	private void changeLtoR() {
		int curTab = tabHost.getCurrentTab();
		int noOfTabs = getTabHost().getTabWidget().getTabCount();
		int nextTab = ((curTab + 1) % noOfTabs);
		tabHost.setCurrentTab(nextTab);

	}

	private void changeRtoL() {
		int curTab = tabHost.getCurrentTab();
		int noOfTabs = getTabHost().getTabWidget().getTabCount();
		int nextTab = ((curTab + noOfTabs - 1) % noOfTabs);
		tabHost.setCurrentTab(nextTab);

	}

	@Override
	public void onGesturePerformed(GestureOverlayView arg0, Gesture gesture) {
		 List<Prediction> predictions = mLibrary.recognize(gesture);
		    if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
		    	String action = predictions.get(0).name;
		    	if("LtoR".equalsIgnoreCase(action)){
		    		changeLtoR();
		    	} else if("RtoL".equalsIgnoreCase(action)){
		    		changeRtoL();
		    	}
		    }
	}



}
